const db = wx.cloud.database();
const app = getApp();
Page({
  data: {
    nianji: 0,
    nianjis: ['请选择您的年级','大一', '大二', '大三', '大四', '研一', '研二', '研三'],
    leixing: 0,
    leixings: ['请选择领取图书类型','公务员', '教师资格', '教师招聘'],
    success:''
  },

  nianjiChange: function (e) {
    console.log('nianji发送选择改变，携带值为', e.detail.value)
    this.setData({
      nianji: e.detail.value
    })
  },
  leixingChange: function (e) {
    console.log('leixing发送选择改变，携带值为', e.detail.value)
    this.setData({
      leixing: e.detail.value
    })
  },
  nameInput: function (e) {
    this.setData({
      name: e.detail.value
    })
    console.log(this.data.name)
  },

  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.geneal
    })

  },

  getPhoneNumber(e) {
    db.collection('userInfo').where({
      _openid: app.globalData.openid,
    }).get({
      success: res => {
        console.log(res.data.length)
        if (res.data.length == 0) {
          var that = this;
          wx.cloud.callFunction({
            name: 'getMobile',
            data: {
              weRunData: wx.cloud.CloudID(e.detail.cloudID),
            }
          }).then(res => {
            that.setData({
              mobile: res.result,
            })
            console.log(res.result)

            wx.request({ //发送请求
              url: "https://zg99.offcn.com/index/biaodan/register/?actid=27930&callback=?",
              data: {
                  phone: res.result,
                  name: that.data.name,
                  fenxiao: that.data.area,
                  geneal: that.data.geneal,
                  nianji: that.data.nianjis[that.data.nianji],
                  leixing: that.data.leixings[that.data.leixing]
              }, //接收参数
              success(res) { //返回
                  var jsonStr = res.data;
                  jsonStr = jsonStr.replace("(", "");
                  jsonStr = jsonStr.replace(")", "");
                  if (typeof jsonStr != 'object') {
                      jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                      var theJson = JSON.parse(jsonStr);
                  };
                  console.log(theJson)
                  that.setData({
                    success:'success',
                  })
                  
              }
          })
            
          }).catch(err => {
            console.error(err);
          });

        } else {
          wx.navigateTo({
            url: '../search/search'
          })
        }
      },
    })
  },

})